﻿using System;
using System.Linq;
using System.ComponentModel;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using System.Collections.Generic;
using QuanLyTiemThuocTay.cls;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;

namespace QuanLyTiemThuocTay.GiaoDien
{
    public partial class frmGiaThuoc : DevExpress.XtraEditors.XtraForm
    {
        QLTTDataContext db = new QLTTDataContext();
        public frmGiaThuoc()
        {
            InitializeComponent();
            btSua.Enabled = btXoa.Enabled = false;
        }

        private void btDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void frmGiaThuoc_Load(object sender, EventArgs e)
        {
            btMoi.Enabled = true;
            btLuu.Enabled = false;
            colGiaNhap.OptionsColumn.ReadOnly = colGiaBanLe.OptionsColumn.ReadOnly = colGiaBanBuon.OptionsColumn.ReadOnly = true;
            //   leLoai.DataSource = new BindingList<LOAI>(db.LOAIs.ToList());
            //  leDVTinh.DataSource = new BindingList<DVTINH>(db.DVTINHs.ToList());
            cbChon.Properties.DataSource = new BindingList<THUOC>(db.THUOCs.ToList());
        }

        private void LoadData()
        {
            gc.DataSource = new BindingList<GIATHUOC>(db.GIATHUOCs.Where(x => x.MaThuoc == cbChon.EditValue.ToString()).ToList());
            gv.RefreshData();
        }

        private void btMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (cbChon.EditValue.ToString() != "")
            {
                gv.AddNewRow();
                gv.FocusedRowHandle = GridControl.NewItemRowHandle;
                gv.FocusedColumn = colGiaNhap;
                gv.ShowEditor();
                btLuu.Enabled = true;
                colGiaNhap.OptionsColumn.ReadOnly = colGiaBanLe.OptionsColumn.ReadOnly = colGiaBanBuon.OptionsColumn.ReadOnly = false;
            }
            else
            {
                clsHam.HienThi(Text, "Vui lòng chọn thuốc/vật tư cần thay đổi đơn giá");
            }
        }

        private void gv_InvalidValueException(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.DisplayError;
            e.ErrorText = "Dữ liệu phải dài hơn 2 ký tự";
        }

        private void gv_ValidatingEditor(object sender, BaseContainerValidateEditorEventArgs e)
        {
            GridView view = sender as GridView;
            int gia = 0;
            Int32.TryParse(e.Value.ToString(), out gia);
            if (gia < 1)
            {
                e.Valid = false;
                return;
            }
        }

        private void btLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.CloseEditor();
            gv.MoveFirst();
            for (int i = 0; i < gv.RowCount; i++)
            {
                var dulieumoi = gv.GetFocusedRow() as GIATHUOC;
                if (dulieumoi != null)
                {
                    //if (dulieumoi.MaDonGia != 0)
                    //{
                    //    db = new QLTTDataContext();
                    //    var dulieucu = db.GIATHUOCs.SingleOrDefault(x => x.MaDonGia == dulieumoi.MaDonGia);
                    //    if (dulieumoi.GiaBanBuon != dulieucu.GiaBanBuon || dulieumoi.GiaBanLe != dulieucu.GiaBanLe || dulieumoi.GiaNhap != dulieucu.GiaNhap)
                    //    {
                    //        dulieucu.GiaBanBuon = dulieumoi.GiaBanBuon;
                    //        dulieucu.GiaBanLe = dulieumoi.GiaBanLe;
                    //        dulieucu.GiaNhap = dulieumoi.GiaNhap;
                    //        db.SubmitChanges();
                    //        clsHam.HienThi(Text, "Cập nhật giá thành công");
                    //    }
                    //}
                    //else 
                    if (dulieumoi.MaDonGia == 0 && dulieumoi.GiaNhap > 0 && dulieumoi.GiaBanLe > 0 && dulieumoi.GiaBanBuon > 0)
                    {
                        db.GIATHUOCs.InsertOnSubmit(dulieumoi);
                        db.SubmitChanges();
                        clsHam.HienThi(Text, "Thêm mới giá thuốc thành công");
                    }
                    else if (dulieumoi.MaDonGia == 0 && (dulieumoi.GiaNhap < 1 || dulieumoi.GiaBanLe < 1 || dulieumoi.GiaBanBuon < 1))
                        clsHam.HienThi(Text, "Kiểm tra dữ liệu nhập");
                }
                gv.MoveNext();
            }
            LoadData();
            btMoi.Enabled = true;
            btLuu.Enabled = false;
            colGiaNhap.OptionsColumn.ReadOnly = colGiaBanLe.OptionsColumn.ReadOnly = colGiaBanBuon.OptionsColumn.ReadOnly = true;
            gv.MoveLast();
        }


        private void btnNap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            colGiaNhap.OptionsColumn.ReadOnly = colGiaBanLe.OptionsColumn.ReadOnly = colGiaBanBuon.OptionsColumn.ReadOnly = true;
            btMoi.Enabled = true;
            btLuu.Enabled = false;
        }

        private void btPhieuTruoc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!gv.IsFirstRow)
                gv.MovePrev();
        }

        private void btPhieuTiepTheo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!gv.IsLastRow)
                gv.MoveNext();
        }

        private void btTroGiup_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MessageBox.Show("Chức năng đang xây dựng.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btIn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog sv = new SaveFileDialog();
            sv.Filter = "Excel 97-2003|*.xls";
            sv.Title = "Lưu...";
            sv.ShowDialog();
            if (sv.FileName != "")
                gv.ExportToXls(sv.FileName);
        }

        private void gv_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            if (cbChon.EditValue != null)
            {
                GridView view = sender as GridView;
                view.SetRowCellValue(e.RowHandle, colMaDonGia, 0);
                view.SetRowCellValue(e.RowHandle, colNgayThayDoi, DateTime.Now);
                view.SetRowCellValue(e.RowHandle, colMaThuoc, cbChon.EditValue.ToString());
            }
        }

        private void cbChon_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void gv_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            try
            {
                ColumnView view = sender as ColumnView;
                int gsi = 0;
                Int32.TryParse(view.GetRowCellValue(e.RowHandle, colGiaBanBuon).ToString(), out gsi);
                if (gsi < 1)
                {
                    e.Valid = false;
                    view.SetColumnError(colGiaBanBuon, "Giá phải lớn hơn 0");
                    gv.FocusedColumn = colGiaBanBuon;
                }
                int gle = 0;
                Int32.TryParse(view.GetRowCellValue(e.RowHandle, colGiaBanLe).ToString(), out gle);
                if (gle < 1)
                {
                    e.Valid = false;
                    view.SetColumnError(colGiaBanLe, "Giá phải lớn hơn 0");
                    gv.FocusedColumn = colGiaBanLe;
                }
                int gnhap = 0;
                Int32.TryParse(view.GetRowCellValue(e.RowHandle, colGiaNhap).ToString(), out gnhap);
                if (gnhap < 1)
                {
                    e.Valid = false;
                    view.SetColumnError(colGiaNhap, "Giá phải lớn hơn 0");
                    gv.FocusedColumn = colGiaNhap;
                }
            }
            catch { e.Valid = false; }
            (sender as GridView).ShowEditor();
        }

        private void gv_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
            (sender as GridView).ShowEditor();
        }

        private void gv_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView view = sender as GridView;
            e.Cancel = !view.IsNewItemRow(view.FocusedRowHandle);
        }
    }
}